package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.UserService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/31 12:30
 */
@Slf4j
@Api(tags = "数据统计相关接口")
@RestController
@RequestMapping(value = "/admin/report")
public class ReportController {
    @Resource
    private OrderService orderService;
    @Resource
    private UserService userService;
    @Resource
    private WorkSpaceService workSpaceService;

    /**
     * 营业额
     *
     * @return
     */
    @ApiOperation(value = "营业额")
    @GetMapping(value = "/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额：{},{}", begin, end);
        TurnoverReportVO turnoverReportVO = orderService.getTurnoverReport(begin,end);
        return Result.success(turnoverReportVO);
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation(value = "用户统计")
    @GetMapping(value = "/userStatistics")
    public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("用户统计：{},{}", begin, end);
        UserReportVO userReportVO = userService.getUserStatistics(begin,end);
        return Result.success(userReportVO);
    }

    /**
     * 订单统计
     * @return
     */
    @ApiOperation(value = "订单统计")
    @GetMapping(value = "/ordersStatistics")
    public Result<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("订单统计：{},{}", begin, end);
        OrderReportVO orderReportVO = orderService.orderStatistics(begin,end);
        return Result.success(orderReportVO);
    }

    /**
     * 商品销量top10
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation(value = "商品销量top10")
    @GetMapping(value = "/top10")
    public Result<SalesTop10ReportVO> salesTop10Report(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("商品销量top10：{},{}", begin, end);
        SalesTop10ReportVO salesTop10ReportVO = orderService.salesTop10Report(begin,end);
        return Result.success(salesTop10ReportVO);
    }
    /**
     * 导出excel报表
     */
    @ApiOperation(value = "导出excel报表")
    @GetMapping(value = "/export")
    public void exportExcel(HttpServletResponse response){
        // 通过HttpServletResponse将文件给前端进行下载
        workSpaceService.exportExcel(response);
    }
}
